/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.common;

import com.sh.ndt.common.entity.EmailContent;
import com.sh.ndt.common.entity.LoginParam;
import com.sh.ndt.exam.entity.TestHistory;
import com.sh.ndt.exam.entity.TestHistoryParam;
import com.sh.ndt.question.entity.QuestionParam;
import com.sh.ndt.schedule.entity.ExamScheduleParam;
import com.sh.ndt.user.entity.UserParam;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.util.JAXBSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.log4j.Logger;
import org.springframework.core.io.Resource;

/**
 *
 * @author prashanth.nc
 */
public class CreateXML implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = Logger.getLogger(CreateXML.class);
    private static Validator validator;
    private static JAXBContext context;
    private static SchemaFactory sf;
    private static Schema schema;
    private static JAXBSource jaxbSource;
    private static Marshaller marshaller;
    private static Resource resource;

    public static String generateLoginParamXML(LoginParam loginParam) {
        StringWriter stringWriter = new StringWriter();
        try {

            /**
             * Marshalling Starts from Here *
             */
            sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            LOGGER.debug("SchemaFactory.newInstance - done" + sf.toString());

            LOGGER.debug("SchemaFactory.newInstance - done");

            context = JAXBContext.newInstance(LoginParam.class);
            LOGGER.debug("JAXBContext.newInstance - done");

            jaxbSource = new JAXBSource(context, loginParam);
            LOGGER.debug("JAXBSource - done");

            marshaller = context.createMarshaller();
            LOGGER.debug("context.createMarshaller - done");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            marshaller.marshal(loginParam, stringWriter);
            LOGGER.debug("Marshelling - done");

        } catch (PropertyException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringWriter.toString();
    }

    public static String generateExamScheduleParamXML(ExamScheduleParam examScheduleParam) {
        StringWriter stringWriter = new StringWriter();
        try {

            /**
             * Marshalling Starts from Here *
             */
            sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            LOGGER.debug("SchemaFactory.newInstance - done" + sf.toString());

            LOGGER.debug("SchemaFactory.newInstance - done");

            context = JAXBContext.newInstance(ExamScheduleParam.class);
            LOGGER.debug("JAXBContext.newInstance - done");

            jaxbSource = new JAXBSource(context, examScheduleParam);
            LOGGER.debug("JAXBSource - done");

            marshaller = context.createMarshaller();
            LOGGER.debug("context.createMarshaller - done");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            marshaller.marshal(examScheduleParam, stringWriter);
            LOGGER.debug("Marshelling - done");

        } catch (PropertyException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringWriter.toString();
    }

    public static String generateUserParamXML(UserParam userParam) {
        StringWriter stringWriter = new StringWriter();
        try {

            /**
             * Marshalling Starts from Here *
             */
            sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            LOGGER.debug("SchemaFactory.newInstance - done" + sf.toString());

            LOGGER.debug("SchemaFactory.newInstance - done");

            context = JAXBContext.newInstance(UserParam.class);
            LOGGER.debug("JAXBContext.newInstance - done");

            jaxbSource = new JAXBSource(context, userParam);
            LOGGER.debug("JAXBSource - done");

            marshaller = context.createMarshaller();
            LOGGER.debug("context.createMarshaller - done");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            marshaller.marshal(userParam, stringWriter);
            LOGGER.debug("Marshelling - done");

        } catch (PropertyException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringWriter.toString();
    }

    public static String generateQuestionParamXML(QuestionParam questionParam) {
       
        StringWriter stringWriter = new StringWriter();
        try {

            /**
             * Marshalling Starts from Here *
             */
            sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            LOGGER.debug("SchemaFactory.newInstance - done" + sf.toString());

            LOGGER.debug("SchemaFactory.newInstance - done");

            context = JAXBContext.newInstance(QuestionParam.class);
            LOGGER.debug("JAXBContext.newInstance - done");

            jaxbSource = new JAXBSource(context, questionParam);
            LOGGER.debug("JAXBSource - done");

            marshaller = context.createMarshaller();
            LOGGER.debug("context.createMarshaller - done");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            marshaller.marshal(questionParam, stringWriter);
            LOGGER.debug("Marshelling - done");

        } catch (PropertyException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringWriter.toString();
    }

    public static String generateEmailContentParamXML(EmailContent emailContent) {
        StringWriter stringWriter = new StringWriter();
        try {

            /**
             * Marshalling Starts from Here *
             */
            sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            LOGGER.debug("SchemaFactory.newInstance - done" + sf.toString());

            LOGGER.debug("SchemaFactory.newInstance - done");

            context = JAXBContext.newInstance(EmailContent.class);
            LOGGER.debug("JAXBContext.newInstance - done");

            jaxbSource = new JAXBSource(context, emailContent);
            LOGGER.debug("JAXBSource - done");

            marshaller = context.createMarshaller();
            LOGGER.debug("context.createMarshaller - done");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            marshaller.marshal(emailContent, stringWriter);
            LOGGER.debug("Marshelling - done");

        } catch (PropertyException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringWriter.toString();
    }

    public static String generateTestHistoryParamXML(TestHistoryParam testHistoryParam) {
        StringWriter stringWriter = new StringWriter();
        try {

            /**
             * Marshalling Starts from Here *
             */
            sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            LOGGER.debug("SchemaFactory.newInstance - done" + sf.toString());

            LOGGER.debug("SchemaFactory.newInstance - done");

            context = JAXBContext.newInstance(TestHistoryParam.class);
            LOGGER.debug("JAXBContext.newInstance - done");

            jaxbSource = new JAXBSource(context, testHistoryParam);
            LOGGER.debug("JAXBSource - done");

            marshaller = context.createMarshaller();
            LOGGER.debug("context.createMarshaller - done");

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            marshaller.marshal(testHistoryParam, stringWriter);
            LOGGER.debug("Marshelling - done");

        } catch (PropertyException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringWriter.toString();    }
    
}
